/**
 * 
 */
package com.ericsson.entironment.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.xml.XMLException;
import com.ericsson.common.xml.XMLParser;



/**
 *
 *Soarland
 *
 *SysParams.java
 *TODO read System parameters from the xml file
 * @author oarsman
 * Apr 19, 2008
 */
public class SysParams
{
	protected static SysParams sysParam=null;
	protected static Object lock=new Object();

	protected Map ports=new HashMap();
	protected int timeout;
	protected Vector<ReceiverBean> msgReceiver=new Vector<ReceiverBean>();
	

	private static Log log=LogFactory.getLog(SysParams.class); 
	private SysParams()
	{
		String path=SysParams.class.getResource("/SystemConfig.xml").getPath();
		int i;
		String name;
		String port;
		ReceiverBean receiver;
		try
		{
			XMLParser parser=new XMLParser(path);
			i=0;
			
			while(true)
			{//读取所有的端口信息
				try
				{
					name=parser.getProperty("commport[0].port["+i+"]","name");
					port=parser.getProperty("commport[0].port["+i+"]","value");
					if (name==null)
						break;
					ports.put(name,port);
					i++;
				}
				catch (XMLException e)
				{
					break;
				}
				
			}
			i=0;
			while(true)
			{//读取所有的消息接收器信息
				try
				{
					if (parser.getElement("MsgReceiver[0].Receiver["+i+"].name[0]")==null)
						break;
					
					receiver=new ReceiverBean(
							parser.getElement("MsgReceiver[0].Receiver["+i+"].name[0]"),
							parser.getElement("MsgReceiver[0].Receiver["+i+"].class[0]"),
							Integer.parseInt(parser.getElement("MsgReceiver[0].Receiver["+i+"].load[0]"))
					);
					i++;
					msgReceiver.add(receiver);
				}
				catch (XMLException e)
				{
					break;
				}
				
			}
			
			this.timeout=Integer.parseInt(parser.getElement("timeout[0]"));
			
		}
		catch (XMLException e)
		{
			log.fatal("\n系统发生错误，无法读取系统配置文件，系统终止\n",e);
			System.exit(-1);
		}
		
	}
	
	public synchronized static SysParams getInstance()
	{
		if (sysParam==null)
		{
			sysParam=new SysParams();
		}
		return sysParam;
	}


	public Vector<ReceiverBean> getMsgReceiver()
	{
		return msgReceiver;
	}

	public int getPorts(String name)
	{
		return Integer.parseInt((String)ports.get(name));
	}

	public int getTimeout()
	{
		return timeout;
	}



	
}
